/**
 * User: alesj
 * Date: 2004.9.7
 * Time: 12:24:22
 */

package com.generalynx.ecos.web.form;

import com.generalynx.common.data.IEntity;
import com.generalynx.common.resources.Resources;
import com.generalynx.common.web.module.Module;
import com.generalynx.common.web.module.Modules;
import com.generalynx.ecos.data.User;

import java.util.List;
import java.util.Set;

public class UserModuleSelectForm extends AbstractSingleSelectForm {

    protected String getHeaderLabel() {
        return Resources.getString(Resources.USER);
    }

    protected String getBodyLabel() {
        return Resources.getString(Resources.MODULES);
    }

    protected Class getEntityClass() {
        return User.class;
    }

    protected Class getMembersClass() {
        return Module.class;
    }

    protected Set getMembers(IEntity entity) {
        return ((User)entity).getModules();
    }

    /**
     * Only show lists.
     */
    protected Set getMutableMembers(IEntity entity) {
        Set members = getWrappedEntitySet(getMembers(entity));
        members.retainAll(getAll());
        return members;
    }

    protected List getAll() {
        return Modules.getLeaves();
    }

    protected void doLeft(int[] left, Set members, IEntity entity) {
        for (int i = 0; i < left.length; i++) {
            Module module = getBasicDAO().loadModule(left[i]);
            while(module != null) {
                members.add(module);
                module = module.getParent();
            }
        }
    }

}
